<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        .container {
            width: 350px;
            height: 300px;
            margin: 0 auto;
            /* border: 1px black solid; */
            text-align: center;
        }

        .grey {
            color: grey;
        }

        .container .row {
            width: 350px;
            height: 40px;

            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .row input {
            width: 260px;
            height: 30px;
        }

        #submit {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
        }
    </style>
</head>

<body>
<div id="container">
    <h1>留言板</h1>
    <p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
    <div class="row">
        <span>谁:</span> <input type="text" name="" id="from">
    </div>
    <div class="row">
        <span>对谁:</span> <input type="text" name="" id="to">
    </div>
    <div class="row">
        <span>说什么:</span> <input type="text" name="" id="say">
    </div>
    <input type="button" value="提交" id="submit" onclick="submit()">
    <!-- <div>A 对 B 说: hello</div> -->

</div><br>
<script src="jquery-3.7.1.js"></script>
<script>
    login();
    function login(){
        $.ajax({
            type: "POST",
            url: "/message/login",

            success: function (result) {
                for(var list of result){
                    var s = "<div>" + list.i + "对" + list.you + "说" + list.talk + "</div>";
                    $("#container").append(s);
                }
            },
        });
    }
    //点击了submit后的操作
    function submit() {
        var Data = {
            i: $("#from").val(),
            you: $("#to").val(),
            talk: $("#say").val()
        };
        $.ajax({
            url: "/message/store",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(Data),
            success: function (list) {
                if (list) {
                    var s = "<div>" + list.i + "对" + list.you + "说" + list.talk + "</div>";
                    $("#container").append(s);
                } else {
                    console.log("服务器返回错误")
                }

            },

        });
    }

</script>
</body>

</html>